﻿using B.S.BasicData.Api.Read.Applications.Command;
using B.S.BasicData.Domains;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructures;
using MediatR;
using System.Runtime.CompilerServices;

namespace B.S.BasicData.Api.Read.Applications.CommandHandler
{
    public class PermissonQueryCommandHandler : IRequestHandler<PermissonQueryCommand, ApiResult<List<Permissons>>>
    {
        private readonly IBaseRepository<Permissons> permissonRepository;

        public PermissonQueryCommandHandler(IBaseRepository<Permissons> permissonRepository)
        {
            this.permissonRepository = permissonRepository;
        }

        public Task<ApiResult<List<Permissons>>> Handle(PermissonQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<Permissons>> result=new ApiResult<List<Permissons>>();
            result.Code = ApiEnum.Success;
            result.Message = "列表权限展示成功";
            var list=permissonRepository.GetAll().Where(x=>x.IsDel==false);
            if(!string.IsNullOrEmpty(request.PermissonName))
            {
                list = list.Where(x => x.PermissonName == request.PermissonName);
            }
            result.data=list.ToList();
            return Task.FromResult(result);
        }
    }
}
